package agu.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextClock;
import java.text.SimpleDateFormat;
import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 支持预览
 * {@link #getFormat24Hour() format24Hour}
 * 或
 * {@link #getFormat12Hour() format12Hour}
 */
public class TextClockView extends TextClock
{

    public TextClockView(@NonNull Context context){
        super(context);
        init(context, null);
    }

    public TextClockView(@NonNull Context context, @Nullable AttributeSet attrs){
        super(context, attrs);
        init(context, attrs);
    }

    public TextClockView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs){
        if(isInEditMode() && TextUtils.isEmpty(getText())){
            long time = System.currentTimeMillis();
            Locale locale = Locale.getDefault();

            String text = new SimpleDateFormat(getFormat24Hour().toString(), locale).format(time);
            String text12 = new SimpleDateFormat(getFormat12Hour().toString(), locale).format(time);
            if(text.length() < text12.length()) text = text12;

            setText(text);
        }
    }
}
